package com.fy56.platform.goodsTracking.web.restful.api.controller;

import com.alibaba.fastjson.JSON;
import com.fy56.kafka.producer.MyProducer;
import com.fy56.platform.goodsTracking.web.restful.api.model.ResultModel;
import com.fy56.platform.goodsTracking.web.restful.api.model.ResultStatus;
import com.wordnik.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * Created by sxg on 2019-11-26.
 */
@RestController
@RequestMapping("/notify")
public class NotifyController {
    private static final Logger log = LoggerFactory.getLogger(NotifyController.class);

    @RequestMapping(value = "/goodsTrackingNotify", method = RequestMethod.POST)
    @ApiOperation(value = "货物跟踪回调通知")
    public ResponseEntity<ResultModel> goodsTrackingNotify(
            @RequestParam(value = "data") String data,
            @RequestParam(value = "Recordid") String Recordid,
            @RequestParam(value = "consignmentbillmasterid") String consignmentbillmasterid) {

        log.info("接收货物跟踪通知数据:data:" + data+",Recordid:"+Recordid+",consignmentbillmasterid:"+consignmentbillmasterid);
        try {
            Map<String, Object> paramMap = JSON.parseObject(data, Map.class);
            paramMap.put("Recordid", Recordid);
            paramMap.put("consignmentbillmasterid", consignmentbillmasterid);
            data = JSON.toJSONString(paramMap);
            //货物跟踪回調通知
            MyProducer.send("f6GoodsTrackingNotifyQueue", data);

            return new ResponseEntity(ResultModel.ok(null), HttpStatus.OK);
        } catch (Exception exception) {
            log.error("参数:" + data + "系统错误:error:" + exception.getMessage());
            return new ResponseEntity(ResultModel.error(ResultStatus.SERVER_ERROR, exception.getMessage()), HttpStatus.OK);
        }
    }
}
